package com.ruoyi.withdrawaltable.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.user.domain.User;
import com.ruoyi.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.withdrawaltable.mapper.WithdrawalTableMapper;
import com.ruoyi.withdrawaltable.domain.WithdrawalTable;
import com.ruoyi.withdrawaltable.service.IWithdrawalTableService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【提现列表】Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-04
 */
@Service
public class WithdrawalTableServiceImpl implements IWithdrawalTableService 
{
    @Autowired
    private WithdrawalTableMapper withdrawalTableMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public WithdrawalTable selectWithdrawalTableById(Long id)
    {
        return withdrawalTableMapper.selectWithdrawalTableById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param withdrawalTable 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<WithdrawalTable> selectWithdrawalTableList(WithdrawalTable withdrawalTable)
    {
        return withdrawalTableMapper.selectWithdrawalTableList(withdrawalTable);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param withdrawalTable 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertWithdrawalTable(WithdrawalTable withdrawalTable)
    {
        return withdrawalTableMapper.insertWithdrawalTable(withdrawalTable);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param withdrawalTable 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateWithdrawalTable(WithdrawalTable withdrawalTable)
    {
        return withdrawalTableMapper.updateWithdrawalTable(withdrawalTable);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteWithdrawalTableByIds(Long[] ids)
    {
        return withdrawalTableMapper.deleteWithdrawalTableByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteWithdrawalTableById(Long id)
    {
        return withdrawalTableMapper.deleteWithdrawalTableById(id);
    }

    /**
     * 修改提现状态
     *
     * @param withdrawalTable
     * @return
     */
    @Override
    @Transactional
    public String updateWithdrawalTableStatus(WithdrawalTable withdrawalTable) {
        String withdrawalStatus = withdrawalTable.getWithdrawalStatus();
        User user = userMapper.selectUserByUserId(withdrawalTable.getUserId());
        // 驳回
        if ("1".equals(withdrawalStatus) || "4".equals(withdrawalStatus)) {
            withdrawalTableMapper.updateWithdrawalTable(withdrawalTable);
            BigDecimal totalAmount = withdrawalTable.getTotalAmount();
            user.setWithdrawableAmount(user.getWithdrawableAmount().add(totalAmount));
            userMapper.updateUser(user);
        } else {
            withdrawalTableMapper.updateWithdrawalTable(withdrawalTable);
        }
        return "修改成功";
    }
}
